<?php
namespace Admin\Controller;
use Think\Controller;
class JiazhangController extends CommonController 
{
	//家长列表
    public function index()
    {
      if (IS_POST) {
        $start = strtotime(I('post.start'));
        $stop = strtotime(I('post.stop'));
        $username = I('post.username');
        $sql = "select id,phone,sex,name,face,city,is_dongjie,create_time from jiazhang_user where is_del=0 and create_time between '{$start}' and '{$stop}' and name like '%{$username}%'";
        $users = M()->query($sql);
        $this->assign('users',$users);
      }else {
        //分页
          $count = M('jiazhang_user')->count();
          $this->assign('num',$count);

          //所有用户显示出来
          $user = M('jiazhang_user');        
          // 查询满足要求的总记录数
          $count =  $user->count();
          $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
          $show       = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性

          //问题显示
          $sql = "select id,phone,sex,name,face,city,is_dongjie,create_time from jiazhang_user where is_del=0 limit ".$Page->firstRow.','.$Page->listRows;
          $users = $user->query($sql);
          $this->assign('users',$users);        
          $this->assign('page',$show);// 赋值分页输出
      }

        $this->display();
    }

    /**
     * 导出 家长列表所有数据
     */
    public function jiazhang_export(){
        $sql = "select id,phone,sex,name,city,is_dongjie,create_time from jiazhang_user where is_del=0";
        $data = M()->query($sql);

        $subject="Excel导出-家长列表";
        $title=array("id","家长手机","家长性别","家长","城市","是否冻结","加入时间");
        exportExcel($data,$title,$subject); 
    }

    //添加显示
    public function add_index()
    {
      //城市显示
      $citys = M('city')->where('parent_id=0')->select();
      $this->assign('citys',$citys);

      $this->display();
    }

    //添加
    public function add()
    {
      //[phone] => 13430372763[username] => 啊啊啊[sex] => 男[city] => 广州[pass] => 111111[repass] => 111111
      if (IS_POST) {
        $data['phone'] = I('post.phone');
        $data['name'] = I('post.username');
        $data['sex'] = I('post.sex');
        $data['city'] = I('post.city');

        //默认头像
        if ($data['sex'] == '男') {
          $data['face'] = "/Uploads/face/man.jpg";
        }else {
          $data['face'] = "/Uploads/face/woman.jpg";
        }
        //手机重复检测
        $shouji_cz = M('jiazhang_user')->where(array('phone'=>$data['phone']))->getField('phone');
        if (!empty($shouji_cz)) {
          $arr = array(
            'error' =>0,
            'info' => '手机已存在'
            );
          echo json_encode($arr);
          exit;
        }

        $password = I('post.pass');
        $data['password'] = md5($password);

        $data['create_time'] = time();
        $data['create_ip'] =  get_client_ip();

        $res = M('jiazhang_user')->add($data);
        if ($res) {
          $arr = array(
            'error' =>1,
            'info' => '添加成功'
            );
          echo json_encode($arr);
        }else {
          $arr = array(
            'error' =>0,
            'info' => '添加失败'
            );
          echo json_encode($arr);
        }
      }
    }

    //编辑显示
    public function edit_index()
    {
      //城市显示
      $citys = M('city')->where('parent_id=0')->select();
      $this->assign('citys',$citys);
      
      $id = I('get.id');
      $sql = "select id,phone,sex,name,city,create_time from jiazhang_user where id='{$id}'";
      $jiazhang = M()->query($sql);
      $this->assign('jiazhang',current($jiazhang));

      $this->display();
    }
   //编辑
    public function edit()
    {
      if (IS_POST) {
        //f$data['phone'] = I('post.phone');
        $data['name'] = I('post.username');
        $data['sex'] = I('post.sex');
        $data['city'] = I('post.city');

        $id = I('post.id');

        //更新
        $res = M('jiazhang_user')->where(array('id'=>$id))->save($data);
            if ($res) {
                $arr = array(
                    'error' => 1,
                    'info' => '更新成功'    
                    );
                echo json_encode($arr);
            }else {
                $arr = array(
                    'error' => 0,
                    'info' => '更新失败'    
                    );
                echo json_encode($arr);
            }
      }
    }

    //家长需求表
    public function jiazhang_xuqiu()
    {
      $id = I('get.id');
      $this->assign('id',$id);

      $city_name = M('jiazhang_user')->where(array('id'=>$id))->getField('city');
      $this->assign('city_name',$city_name);

      $cityid = M('city')->where(array('city_name'=>$city_name))->getField('cityid');

      //对应区域
      $city_quyu = M('city')->where(array('parent_id'=>$cityid))->select();
      $this->assign('city_quyu',$city_quyu);

      //上课年级
      $grades = M('grade')->where('parent_id=0')->select();
      $this->assign('grades',$grades);

      //需求信息
      $jiazhang_xuqiu = M('jiazhang_xuqiu')->where(array('uid'=>$id))->find();
      $this->assign('jiazhang_xuqiu',$jiazhang_xuqiu);

      $jiazhang_xuqiu_subjects = explode(',', $jiazhang_xuqiu['subjects']);
      $this->assign('jiazhang_xuqiu_subjects',$jiazhang_xuqiu_subjects);
      //科目显示
      $kecheng = M('subjects')->select();
      $this->assign('kecheng',$kecheng);


      $this->display();
    }

    //家长需求表 添加 更新
    public function jiazhang_xuqiu_add()
    {
      if (IS_POST) {
        $data['sk_city'] = I('post.sk_city');
        $data['sk_quyu'] = I('post.quyu');
        $data['name'] = I('post.name');
        $data['sex'] = I('post.sex');
        $data['grade'] = I('post.grade');

        $kecheng = I('post.kecheng');
        $data['subjects'] = implode(',', $kecheng);

        $data['uid'] = I('post.uid');

        $uid = M('jiazhang_xuqiu')->where(array('uid'=>$data['uid']))->getField('uid');
        if ($uid) {
          $res = M('jiazhang_xuqiu')->where(array('uid'=>$data['uid']))->save($data);
          if ($res) {
               $arr = array(
                      'error' =>1,
                      'info'=>"更新成功"
                      );
              echo json_encode($arr);
          }else {
               $arr = array(
                      'error' =>0,
                      'info'=>"更新失败"
                      );
              echo json_encode($arr);
          }
        }else {
          $res = M('jiazhang_xuqiu')->add($data);
          if ($res) {
               $arr = array(
                      'error' =>1,
                      'info'=>"添加成功"
                      );
              echo json_encode($arr);
          }else {
               $arr = array(
                      'error' =>0,
                      'info'=>"添加失败"
                      );
              echo json_encode($arr);
          }
        }
      }

    }


    //删除状态 0未删除 1删除
    public function is_del()
    {
      if (IS_POST) {
          $userid = I('post.userid');
          $sql = 'update jiazhang_user set is_del=1 where id='.$userid;
          $is_denglu = M()->execute($sql);
          if ($is_denglu) {
              $arr = array(
                  'error' =>0,
                  'info'=>"用户删除成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>1,
                  'info'=>"用户删除失败"
                  );
              echo json_encode($arr);
          }
        }       
    }

    //批量删除
    public function deletes()
    {
      //Array ( [arr] => Array ( [0] => 1 [1] => 2 ) )
      if (IS_POST) {
        $arr = I('post.arr');
        $ids = implode(",", $arr);
        $w['id']=array('in',"$ids");
        
        $res = M('jiazhang_user')->where($w)->setField('is_del',1);
          if ($res) {
              $arr = array(
                  'error' =>0,
                  'info'=>"用户删除成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>1,
                  'info'=>"用户删除失败"
                  );
              echo json_encode($arr);
          }
      }
    }


    //删除老师列表
    public function del_index()
    {
      if (IS_POST) {
        $start = strtotime(I('post.start'));
        $stop = strtotime(I('post.stop'));
        $username = I('post.username');
        $sql = "select id,phone,sex,name,city,is_dongjie,create_time from jiazhang_user where is_del=1 and create_time between '{$start}' and '{$stop}' and name like '%{$username}%'";
        $users = M()->query($sql);
        $this->assign('users',$users);
      }else {
        //分页
          $count = M('jiazhang_user')->count();
          $this->assign('num',$count);

          //所有用户显示出来
          $user = M('jiazhang_user');        
          // 查询满足要求的总记录数
          $count =  $user->count();
          $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
          $show       = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性

          //问题显示
          $sql = "select id,phone,sex,name,city,is_dongjie,create_time from jiazhang_user where is_del=1 limit ".$Page->firstRow.','.$Page->listRows;
          $users = $user->query($sql);
          $this->assign('users',$users);        
          $this->assign('page',$show);// 赋值分页输出
      }

        $this->display();
    }

    //批量恢复
    public function recovers()
    {
        $arr = I('post.arr');
        $ids = implode(",", $arr);
        $w['id']=array('in',"$ids");
        
        $res = M('jiazhang_user')->where($w)->setField('is_del',0);
        if ($res) {
          $arr = array(
              'error' => 0,
              'info' => '恢复成功'
            );
          echo json_encode($arr);
        }else {
          $arr = array(
              'error' => 1,
              'info' => '恢复失败'
            );
          echo json_encode($arr);
        }
    }

    //删除状态为0的用户 恢复  改为1
    public function no_del()
    {
      if (IS_POST) {
          $userid = I('post.userid');
          $sql = 'update jiazhang_user set is_del=0 where id='.$userid;
          $is_del = M()->execute($sql);
          if ($is_del) {
              $arr = array(
                  'error' =>0,
                  'info'=>"用户恢复成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>1,
                  'info'=>"用户恢复失败"
                  );
              echo json_encode($arr);
          }
        }  
    }


    //彻底删除
    public function delete()
    {
      if (IS_POST) {
          $userid = I('post.userid');
          $sql = 'delete from jiazhang_user where id='.$userid;
          $is_del = M()->execute($sql);
          if ($is_del) {
              $arr = array(
                  'error' =>0,
                  'info'=>"用户彻底删除成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>1,
                  'info'=>"用户彻底删除失败"
                  );
              echo json_encode($arr);
          }
      }  
    }




}